package com.xfly.app.ui;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.utils.AllUtils;
import com.ptmall.app.utils.GlideUtil;
import com.ptmall.app.view.PullToRefreshLayout;
import com.ptmall.app.view.PullableListView;
import com.ptmall.app.view.TitleBarView;
import com.xfly.app.adapter.JhAdapter;
import com.xfly.app.adapter.JhCarAdapter;
import com.xfly.app.bean.CarBean;
import com.xfly.app.bean.DetilBean;
import com.xfly.app.bean.KcBean;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JhCarActivity extends BaseMvpActivity {

    ListView listview;
    TitleBarView title_bar;
    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();

    JhCarAdapter kcAdapter;

    List<CarBean.GoodsBean> list = new ArrayList<>();

    int page = 1;

    PopupWindow popupWindow;
    private View popView;
    String status = "2";

    TextView carnum;


    ImageView goodimg, gb;
    TextView goodprice, kcnum, shopcar_num, add;
    TagFlowLayout id_flowlayout;
    RelativeLayout shopcar_jia, shopcar_jian;
    String specid = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_jhcar);
    }

    @Override
    public void initViews() {
        title_bar = findViewById(R.id.title_bar);
        listview = findViewById(R.id.listview);
        shopcar_jian = findViewById(R.id.shopcar_jian);
        shopcar_jia = findViewById(R.id.shopcar_jia);
        carnum = findViewById(R.id.carnum);
        title_bar.setText("进货清单");
        title_bar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

        kcAdapter = new JhCarAdapter(getContext(), list);
        listview.setAdapter(kcAdapter);
        kcAdapter.setClick(new JhCarAdapter.Click() {
            @Override
            public void Click(String s,String id) {
                if(s.equals("+")){
                    add(id);
                }else {
                    jian(id);
                }
            }

            @Override
            public void Click2(int position) {
            }
        });
        findViewById(R.id.js).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(),TjActivity.class));
                finish();
            }
        });
        clist();
    }

    @Override
    public void initEvents() {

    }

    private void add(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("specs_id", id);
        map.put("mold", "2");
        apiDataRepository.add(map, new ApiNetResponse<Object>(getContext()) {
            @Override
            public void onSuccess(Object data) {

                clist();
            }
        });
    }
    private void jian(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("cart_id", id);
        apiDataRepository.jian(map, new ApiNetResponse<Object>(getContext()) {
            @Override
            public void onSuccess(Object data) {

                clist();
            }
        });
    }


    private void clist() {
        Map<String, String> map = new HashMap<>();
        map.put("mold", "2");
        apiDataRepository.clist(map, new ApiNetResponse<CarBean>(getContext()) {
            @Override
            public void onSuccess(CarBean data) {
                kcAdapter.pushData(data.getGoods());
                carnum.setText("共"+data.getCount()+"件，合计：￥"+data.getTotal());
            }
        });
    }


    /**
     * mFlowLayout.setAdapter(new TagAdapter<String>(mVals)
     *    {
     *        @Override
     *        public View getView(FlowLayout parent, int position, String s)
     *        {
     *            TextView tv = (TextView) mInflater.inflate(R.layout.tv,
     *                    mFlowLayout, false);
     *            tv.setText(s);
     *            return tv;
     *        }
     *    });
     *
     *    <?xml version="1.0" encoding="utf-8"?>
     * <selector xmlns:android="http://schemas.android.com/apk/res/android">
     *     <item android:color="@color/tag_select_textcolor"
     *           android:drawable="@drawable/checked_bg"
     *           android:state_checked="true"></item>
     *     <item android:drawable="@drawable/normal_bg"></item>
     * </selector>
     *
     * */


}
